﻿var  highlightcolor='#c1ebff';
var  clickcolor='#51b2f6';
var cs=null;
function  changeto(){
    source=event.srcElement;
    if  (source.tagName=="TR"||source.tagName=="TABLE")return;
    while(source.tagName!="TD")
        source=source.parentElement;
    source=source.parentElement;
    cs  =  source.children;
    if  (cs[1].style.backgroundColor!=highlightcolor&&source.id!="nc"&&cs[1].style.backgroundColor!=clickcolor)
    for(i=0;i<cs.length;i++){
	    cs[i].style.backgroundColor=highlightcolor;
    }
}
function  changeback(){
    if(event.fromElement.contains(event.toElement)||source.contains(event.toElement)||source.id=="nc")return;
    if(!cs)return;
    if(cs[1]==null)return; 
    if(event.toElement!=source&&cs[1].style.backgroundColor!=clickcolor){
         for(i=0;i<cs.length;i++){
            if(cs[i]){
	            cs[i].style.backgroundColor="#fff";
	        } 
         }
    }
}
function  clickto(){
    source=event.srcElement;
    if  (source.tagName=="TR"||source.tagName=="TABLE")return;
    while(source.tagName!="TD")
        source=source.parentElement;
    source=source.parentElement;
    cs  =  source.children;
    if  (cs[1].style.backgroundColor!=clickcolor&&source.id!="nc"){
        for(i=0;i<cs.length;i++){
	        cs[i].style.backgroundColor=clickcolor;
        }
    }else{
        for(i=0;i<cs.length;i++){
	        cs[i].style.backgroundColor="#fff";
        }
    }
}
var filelist=new Array();
Array.prototype.remove=function(dx){
    if(isNaN(dx)||dx>this.length){return false;}
    for(var i=0,n=0;i<this.length;i++)
    {
        if(this[i]!=this[dx])
        {
            this[n++]=this[i];
        }
    }
    this.length-=1;
}
function PlayBack() {
    var files = GetPlayFile();
    var parent = window.dialogArguments;
    var Player = parent.document.getElementById("NvsPlayer");
    try {
        if (files == "")
            Player.Action("PlayBack");
        else
            Player.Action("PlayBack:" + files);
    }
    catch (e) { }
}
function GetPlayFile(){
    var child=window.frames["list"].document.getElementsByTagName("input");
    var files="";
    for(var i=0;i<child.length;i++){
        if (child[i].type == "checkbox") {
            if (child[i].checked == true && child[i].id != "checkbox1")
                files += GetFileName(child[i]) + "&" + GetFileSize(child[i]) + "&";
        }
    }
    return files;  
}
function GetFileName(obj){
    var filename = "";
    var _element = obj.parentElement.nextSibling;
    if(_element.tagName == "TD" || _element.tagName == "td"){
       filename=_element.childNodes[0].innerHTML;
    }
    return filename;
} 
function GetFileSize(obj){
    var filesize="";
    var _element=obj.parentElement.nextSibling.nextSibling;
    if(_element.tagName=="TD"||_element.tagName=="td"){
        filesize =_element.innerHTML;
    }
    return filesize;
}
function PlayBackByRows(obj){  
    if(obj.childNodes[1].innerHTML!="") {
    	var filename=obj.childNodes[1].childNodes[0].innerHTML;
    	if (filename.split(".")[1] == "avi" || filename.split(".")[1] == "mp4") {
	        filename+="&"+obj.childNodes[2].innerHTML+"&";
	        var parent=window.dialogArguments;
	        var Player = parent.document.getElementById("NvsPlayer");
	        try{
	            Player.Action("PlayBack:"+filename);
	        }catch(e){}
      	}else{
      		var URL=obj.childNodes[1].childNodes[0].href;
      		window.open(URL);
      	}
   }
}
function Download() {
    var files = GetPlayFile();
    var parent = window.dialogArguments;
    var Player = parent.document.getElementById("NvsPlayer");
    try {
        if (files=="")
            alert("Please select files to download");
        else
            Player.Action("DownLoad:" + files);
    } catch (e) { }
}
function DownloadByRows() {
    var obj = event.srcElement;
    if (obj.innerHTML != "") {
        var filename = obj.innerHTML;
        var parent = window.dialogArguments;
        var Player = parent.document.getElementById("NvsPlayer");
        try {
            filename += "&" + obj.parentElement.nextSibling.innerHTML + "&";
            Player.Action("DownLoad:" + filename);
        } catch (e) { }
    }   
}
function SelectAll(){
    var obj=event.srcElement;
    var x=document.getElementsByTagName("input");
    if(obj.checked==true){ 
        for(var i=0;i<x.length;i++){
            if(x[i].type=="checkbox" && x[i].id != "checkbox1")
                x[i].checked=obj.checked;
        }
    }else{
        for(var i=0;i<x.length;i++){
            if(x[i].type=="checkbox" && x[i].id != "checkbox1")
                x[i].checked=obj.checked;
        }
    } 
}
function GetToday(){
    var today = new Date();    
    var day = today.getDate(); 
    day=day>9?day:("0"+day);
    var month = today.getMonth() + 1;
    month=month>9?month:("0"+month);
    var year = today.getYear();    
    var date = year + "-" + month + "-" + day;    
    /*var hour=today.getHours();
    hour=hour>9?hour:("0"+hour); 
    var min=today.getMinutes();
    min=min>9?min:("0"+min);
    var sec=today.getSeconds();
    sec=sec>9?sec:("0"+sec);*/
    document.getElementById("todate").value=date+" 23:59:59"; 
}
function GetYesterday(){
    var yesterday=new Date(); 
    var year=yesterday.getYear(); 
    var month=yesterday.getMonth(); 
    var day=yesterday.getDate(); 
    var date = new Date(year,month,day); 
    date.setDate(date.getDate()-1);
    month=date.getMonth()+1; 
    month=month >9?month:("0"+month) 
    day=date.getDate(); 
    day=day >9?day:("0"+day) 
    date = date.getFullYear() + "-" + month + "-" + day; 
    var hour=yesterday.getHours();
    hour=hour>9?hour:("0"+hour); 
    var min=yesterday.getMinutes();
    min=min>9?min:("0"+min);
    var sec=yesterday.getSeconds();
    sec=sec>9?sec:("0"+sec);
    document.getElementById("fromdate").value=date+" "+hour+":"+min+":"+sec; 
}
function ClearGrid(){
    var _grid = document.getElementById("recordList");
    document.getElementById("checkbox1").checked = "";
    for(var i=1;i<=10;i++)
        for(var j=0;j<5;j++)
            _grid.rows[i].cells[j].innerHTML=""; 
}
function LoadGrid(){   
   try{ 
        if(videolist.length){         
            ClearGrid();
            LoadData(0);
            document.getElementById("record").innerHTML=videolist.length;        
        }else{
            alert("Specified file not found"); 
        } 
   } catch(e){alert("Query error，please retry");}
}
function LoadData(n){
    n*=10;
    if(n<videolist.length) {
        LoadInGrid(n);
    }else{
        LastPage();
    }
}
function NextPage() {
    var n = parseInt(document.getElementById('pageIndex').value) + 1;
    if (10 * n >= videolist.length) {
        alert("The last page is reached");
        return;
    }
    ClearGrid();
    LoadData(n);
    document.getElementById('pageIndex').value = n;
}
function PrevPage() {
    var n = parseInt(document.getElementById('pageIndex').value) - 1;
    if (n < 0) {
        alert("The first page is reached");
        return;
    }
    ClearGrid();
    LoadData(n);
    document.getElementById('pageIndex').value = n;
}
function FstPage() {
    ClearGrid();
    LoadData(0);
    document.getElementById('pageIndex').value = 0;
}
function LastPage() {
    var n = videolist.length / 10;
    if (n > parseInt(n))
        n = parseInt(n);
    else if (n == parseInt(n))
        n -= 1;
    document.getElementById('pageIndex').value = n;
    n = 10 * n ;
    ClearGrid();
    LoadInGrid(n);
}
function LoadInGrid(n){
    try {        
        var URL= window.location.hostname;
        for (var i = 1; i < 11; i++) {
            var _grid = document.getElementById("recordList");
            var filename = videolist[n][0];
            //var disk = filename.split("_")[0];
            //var date = filename.substring(2,10);
            //var chnId = filename.split("_")[2].substring(0,2);
            var truename = filename.substring(2);
            _grid.rows[i].cells[0].innerHTML = "<input type='checkbox'/>" + (n + 1).toString();
            //_grid.rows[i].cells[1].innerHTML = "<a href='ftp://hc-nvs:123789@"+URL+"/"+disk+"/files/"+date+"/"+chnId+"/"+truename+"'>"+filename+"</a>";
            _grid.rows[i].cells[1].innerHTML = "<a href='javascript:void(0)' onclick='DownloadByRows()'>" + filename + "</a>";
            _grid.rows[i].cells[2].innerHTML = videolist[n][1];
            _grid.rows[i].cells[3].innerHTML = videolist[n][2];
            _grid.rows[i].cells[4].innerHTML = videolist[n][3];
            n++;
            if (n == videolist.length) break;
        }
   }catch(e){} 
}
function GotoNewPage(){
    var n=parseInt(document.getElementById('page').value);
    if(n>=0){                
        if(10*n>videolist.length)
            n=parseInt(Math.floor(videolist.length)/10)*10;  
        document.getElementById('pageIndex').value=n/10;
        ClearGrid();
        LoadData(n);
    }
}
function OpenDlg(page, status) {
    status += 'scrollbars=no;resizable=no;help=no;status=no;location=no;';
    window.showModalDialog(page, this, status);
}
function Checkdate(){
    var fromdate=document.getElementById("fromdate").value;
    var todate=document.getElementById("todate").value; 
    var year1=fromdate.split("-")[0];
    var month1=fromdate.split("-")[1]; 
    var year2=todate.split("-")[0];
    var month2=todate.split("-")[1]; 
    if(year1!=year2){
        alert("Query date interval too long"); 
        return false;
    }  else if(month1<(month2-1)){
        alert("Query date interval error");
        return false;
    }        
    return true;     
}